package ATBMS.ucm;

import ATBMS.control.AdminControl;
import ATBMS.control.LoginControl;
import ATBMS.control.MainApp;
import ATBMS.control.ManagerControl;
import ATBMS.control.UserControl;
import ATBMS.modles.DAO.DAOFactory;
import ATBMS.modles.DAO.UserDAO;
import ATBMS.modles.User;
import ATBMS.view.admin.AdminViewImpl;
import ATBMS.view.manager.ManagerViewImpl;
import ATBMS.view.user.UserViewImpl;

/**
 * Login User Use Case Implement
 */
public class UCLoginUser implements UCController {

    private LoginControl lc;

    public UCLoginUser(LoginControl lc) {
        this.lc = lc;
    }

    public void run() {
        User user = lc.getLoginView().getInput();
        if (user == null) {
            lc.getLoginView().alert("Please input username and password");
            return;
        }

        UserDAO userdao = DAOFactory.getUserDAO();
        if (userdao == null) {
            lc.getLoginView().alert("Can't Connect To DB!");
            return;
        }
        if ((user = userdao.validateUser(user)) == null) {
            lc.getLoginView().alert("User Name or Password Wrong!");
            return;
        }
        lc.getLoginView().close();
        if (user.getUflag() == User.USERLEVEL_USER) {
            UserControl uc = UserControl.getInstance(user);
            UserViewImpl uv = new UserViewImpl(MainApp.getApplication(), uc);
            uc.setUserView(uv);
            uv.getFrame().setTitle("AirlineTicketBookingManageSystem(User)");
            uv.show();
        } else if (user.getUflag() == User.USERLEVEL_MANAGER) {
            ManagerControl mc = new ManagerControl();
            ManagerViewImpl mv = new ManagerViewImpl(MainApp.getApplication(), mc);
            mc.setManagerView(mv);
            mv.getFrame().setTitle("AirlineTicketBookingManageSystem(Manager)");
            mv.show();
        } else if (user.getUflag() == User.USERLEVEL_ADMIN) {
            AdminControl adminctrl = new AdminControl();
            AdminViewImpl adminview = new AdminViewImpl(MainApp.getApplication(), adminctrl);
            adminctrl.setAdminView(adminview);
            adminview.getFrame().setTitle("AirlineTicketBookingManageSystem(Admin)");
            adminview.show();
        }
    }
}
